package com.nado.iims2.blog.bo.notice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nado.iims2.blog.dao.notice.NoticeDAO;
import com.nado.iims2.blog.model.notice.Notice;
import com.nado.iims2.blog.util.CommonUtil;

@Service("NoticeBO")
public class NoticeBOImpl implements NoticeBO {
	private static Log log = LogFactory.getLog(NoticeBOImpl.class);
	
	@Autowired
	private NoticeDAO noticeDAO = null;
	
	@Override
	public List<Notice> getNoticeList(int offset, int count) {
		try {
			List<Notice> result = new ArrayList<Notice>();
			result = noticeDAO.selectNoticeList(offset, count);
			return result;
		} catch (Exception e) {
			log.error(e, e);
			return new ArrayList<Notice>();
		}
	}

	@Override
	public int getNoticeListCount() {
		try {
			return noticeDAO.selectNoticeListCount();
		} catch (Exception e) {
			log.error(e, e);
			return 0;
		}
	}

	@Override
	public Notice getNotice(int noticeNo) {
		try {
			Notice result = new Notice();
			result = noticeDAO.selectNotice(noticeNo);
			return result;
		} catch (Exception e) {
			log.error(e, e);
			return null;
		}
	}

	@Override
	public int writeNotice(Notice model) {
		int reuslt = 0;
		try {
			model.setContents(CommonUtil.formatContent(model.getContents()));
			
			reuslt = noticeDAO.insertNotice(model);
			return reuslt;
		} catch (Exception e) {
			log.error(e, e);
			return -1;
		}
	}
}
